package com.sunlg.smarthome.ui.light;

import android.content.Context;
import android.icu.util.BuddhistCalendar;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.contract.LightContract;
import com.sunlg.smarthome.data.entity.Light;

import java.util.List;

/**
 * Created by sunlg on 2017/4/27.
 */

public class LightAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<Light> mLightList;

    public LightAdapter(Context context, List<Light> lightList) {
        this.mContext = context;
        this.mLightList = lightList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_light, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ItemViewHolder viewHolder = (ItemViewHolder) holder;

        final Light light = mLightList.get(position);

        if(light.getStatus() == 0){   //判断照明设备状态
            viewHolder.switchStauts.setChecked(false);
        }else if(light.getStatus() > 0){
            viewHolder.switchStauts.setChecked(true);
        }
        viewHolder.tvDesc.setText(light.getDescription());

        final LightListActivity lightListActivity = (LightListActivity) mContext;
        final LightContract.Presenter presenter = lightListActivity.presenter;

        //switch监听
        viewHolder.switchStauts.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    presenter.takeOn(light.getId(), 50);
                }else {
                    presenter.takeOff(light.getId());
                }
            }
        });

        //item长按监听
        viewHolder.rlLightItem.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                presenter.deleteLight(light.getId());
                return false;
            }
        });

        //item单击事件
        viewHolder.rlLightItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("light", light);
                bundle.putInt("pos", position);
                lightListActivity.overlay(LightActivity.class, bundle);
            }
        });

    }

    @Override
    public int getItemCount() {
        return this.mLightList.size();
    }

    private static class ItemViewHolder extends RecyclerView.ViewHolder{
        TextView tvDesc;
        Switch switchStauts;
        RelativeLayout rlLightItem;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvDesc = (TextView)itemView.findViewById(R.id.tv_desc);
            switchStauts = (Switch)itemView.findViewById(R.id.switch_status);
            rlLightItem = (RelativeLayout)itemView.findViewById(R.id.rl_list_item);
        }
    }

}
